const Mock = require('mockjs');

// post请求：模拟后台数据
const res = Mock.mock({
    total: 100,
    'list|100': [{
        id: '@id',
        name: '@cname',
        productLine: '@ctitle',
        trade: '@ctitle',
        createTime: '@datetime'
    }]
})

let resList;

module.exports = Mock.mock(/\/list.*/, 'post', (options) => {
    const {
        name,
        productLine,
        trade,
        createTime,
        pageIndex,
    } = JSON.parse(options.body);

    if (pageIndex > 10) {
        return {
            status: 200,
            data: []
        }
    }

    if (name || productLine || trade || createTime) {
        res.list = res.list.filter(v => {
            return (
                (name && v.name === name) ||
                (productLine && v.productLine === productLine) ||
                (trade && v.trade === trade) ||
                (createTime && v.createTime === createTime)
            )
        })
    }
    
    const list = res.list.slice(pageIndex - 1, 9 + pageIndex);

    resList = JSON.parse(JSON.stringify(list));

    return {
        status: 200,
        data: {
            total: res.total,
            list: list
        }
    }
})

module.exports = Mock.mock(/\/delete.*/, 'post', (options) => {
    const id = JSON.parse(options.body)?.id;
    const deleteIndex = resList.findIndex(v => {
        return v.id === id;
    });
    
    if (deleteIndex >= 0) {
        resList.splice(deleteIndex, 1);
    }

    return {
        status: 200,
        data: {
            total: res.total,
            list: resList
        }
    }
})

module.exports = Mock.mock(/\/info/, 'post', (options) => {
    const basicInfo = Mock.mock({
        info: [{
            title: '用户名',
            value: '@cname'
        }, {
            title: '用户id',
            value: '@id',
        }, {
            title: '公司网站',
            value: '@url',
            link: true
        }, {
            title: '资质',
            value: '大陆企业单位'
        }, {
            title: '一级行业',
            value: '互联网'
        }, {
            title: '二级行业',
            value: '网络服务'
        }, {
            title: '用户类型',
            value: '普通'
        }, {
            title: '批注',
            value: '这真是一个很棒平台呢'
        }]
    });

    return {
        status: 200,
        data: basicInfo
    }
})

module.exports = Mock.mock(/\/detailgrid/, 'post', (options) => {
    const detailGrid = Mock.mock({
        'grid|10': [{
            title: '@ctitle',
            des: '@ctitle',
            picture: 'picture',
            resPage: '@url',
            key: '@id',
            id: '@id'
        }]
    });

    return {
        status: 200,
        data: detailGrid
    }
})
